﻿namespace Vkontakte.Api.Operations
{
	[ApiMethod("friends.get")]
	public sealed class FriendsGet
		: Base30ApiOperation
	{
		private long[] _shortAnswer = null;
		private Person[] _longAnswer = null;


		public FriendsGet()
		{
			NameCase = NameCase.Nom;
		}

		protected override void Compile()
		{
			AddParameter("method", "friends.get");
			if (NameCase!=NameCase.Nom)
			{
				AddParameter("name_case", NameCase.ToStr());
			}
			if (PersonProperties!=PersonProperties.None)
			{
				AddParameter("fields", PersonProperties.ToStr());
			}
			else
			{
				AddParameter("format", "json");
			}
			
			base.Compile();
		}

		protected override void ParseData(string data)
		{
			if (PersonProperties==PersonProperties.None)
			{
				int start = data.IndexOf('[');
				int end = data.IndexOf(']');
				string str = data.Substring(start + 1, end - start-1);
				string[] strNums=str.Split(',');
				_shortAnswer = new long[strNums.Length];
				for (int i = 0; i < strNums.Length;i++ )
				{
					_shortAnswer[i] = long.Parse(strNums[i]);
				}
					//GetFriendsUidResponce responce = Serializer.LoadObjectXmlString<GetFriendsUidResponce>(data);
					//_shortAnswer = responce.Uids;
			}else
			{
				ListingResponce responce = Serializer.LoadObjectXmlString<ListingResponce>(data);
				for (int i=0;i<responce.Persons.Length;i++)
				{
					responce.Persons[i].NickName=responce.Persons[i].NickName.Replace("&quot;", "");
					_longAnswer = responce.Persons;
				}
			}
			base.ParseData(data);
		}

		public NameCase NameCase { get; set; }

		public PersonProperties PersonProperties { get; set; }



		public long[] GetShortAnswer()
		{
			return _shortAnswer;
		}

		public Person[] GetLongAnswer()
		{
			return _longAnswer;
		}
	}
}